<?php

class DashboardController extends Zend_Controller_Action {

    private $_isUserAdmin = null;
    private $_userId = null;

    public function init() {
        if (Zend_Auth::getInstance()->hasIdentity()) {
            $validations = new Validations_User();
            $this->_userId = $validations->getUserId();
            $layout = $validations->getUserLayout();
            if ($layout == "admin") {
                $this->_isUserAdmin = true;
            }
            $this->_helper->layout->setLayout($layout);
        } else {
            $this->_redirect("/home");
        }
    }

    public function indexAction() {
        // action body
    }

    public function usersAction() {
        $usersDb = new Application_Model_Users();
        $fetchAll = $usersDb->fetchAll();
        $this->view->fetchAll = $fetchAll;
    }

    public function usersaddAction() {
        $form = new Application_Form_Createuser();
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $values = $form->getValues();
                $data = array(
                    'email' => $values['email'],
                    'password' => md5($values['password']),
                    'user_type' => $values['type'],
                    'details' => json_encode(array('name' => $values['name'], 'details' => $values['details'], 'profileUrl' => $values['profileUrl'])));
                $userDb = new Application_Model_Users();
                $userDb->createNew($data);
                $this->view->isSuccess = true;
            }
        }
    }

    public function usereditAction() {
        $userId = $this->getRequest()->getParam("id");
        $userDb = new Application_Model_Users();
        $user = $userDb->getById($userId);
        $form = new Application_Form_Createuser();
        $form->name->setValue($user['name']);
        $form->type->setValue($user['user_type_id']);
        $form->email->setValue($user['email']);
        $form->password->setRequired(false);
        $form->details->setValue($user['details']);
        $form->profileUrl->setValue($user['profileUrl']);
        $form->id->setValue($user['id']);
        $form->submit->setLabel("Update User Profile");
        if (!$this->_isUserAdmin) {
            $form->email->setAttribs(array('readonly' => 'readonly'));
            $form->password->setAttribs(array('readonly' => 'readonly'));
        }
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $values = $form->getValues();
                $data = array(
                    'details' => json_encode(array('name' => $values['name'], 'details' => $values['details'], 'profileUrl' => $values['profileUrl'])));
                if ($this->_isUserAdmin) {
                    $data['user_type'] = $values['type'];
                    $data['email'] = $values['email'];
                    if ($values['password'] != null) {
                        $data['password'] = md5($values['password']);
                    }
                }
                $userDb = new Application_Model_Users();
                $userDb->updateUser($data, $values['id']);
                $this->view->isSuccess = true;
            }
        }
    }

    public function cloudinaryAction() {
        $optionsDb = new Application_Model_Options();
        $cloudianryDetails = $optionsDb->getCloudianryDetails();
        $form = new Application_Form_Cloudinary();
        $form->cloudName->setValue($cloudianryDetails['cloudName']);
        $form->apiKey->setValue($cloudianryDetails['apiKey']);
        $form->apiSecret->setValue($cloudianryDetails['apiSecret']);
        $form->id->setValue($cloudianryDetails['id']);
        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $values = $form->getValues();
                $array = array(
                    'cloudName' => $values['cloudName'],
                    'apiKey' => $values['apiKey'],
                    'apiSecret' => $values['apiSecret'],
                );
                $optionsDb->updateDetails(array(
                    'details' => json_encode($array),
                        ), $values['id']);
                $this->view->isSuccess = true;
            }
        }
    }

}